<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <script>

        // 向ArrayBuffer赋值
        let ab = new ArrayBuffer(9);
        console.log(ab.byteLength);
        let dataview = new DataView(ab);
        dataview.setUint8(0, 3);
        dataview.setUint8(1, 4);
        // ArrayBuffer转换为typeArray
        for (let elem of new Uint8Array(ab))
            console.log(elem);

        // 获取typeArray的ArrayBuffer对象
        console.log((new Uint8Array(ab)).buffer)

        // 对一个完整的命令（二进制ArrayBuffer）进行分包，每个包为蓝牙单词发送的最大限制
        function chunks(data) {

            let chunks = [];
            let mtu = 20;
            let uin8array = new Uint8Array(data);

            for (let i = 0; i < uin8array.length; i += mtu) {
                let chunk = uin8array.slice(i, i + mtu);
                chunks.push(chunk.buffer);
            }

            return chunks

        }
        console.log((chunks(ab)))
    </script>
</body>

</html>